//
//  NSDate+NNDateFormat.m
//  TestObjectProduct
//
//  Created by fangshufeng on 16/9/29.
//  Copyright © 2016年 fangshufeng. All rights reserved.
//

#import "NSDate+NNDateFormat.h"

@implementation NSDate (NNDateFormat)

+ (NSString *)stringWithWeek:(NSInteger)weekDayNum {
    NSString *weekStr = @"";
    
    switch (weekDayNum) {
        case 1:
            weekStr = @"周日";
            break;
        case 2:
            weekStr = @"周一";
            break;
        case 3:
            weekStr = @"周二";
            break;
        case 4:
            weekStr = @"周三";
            break;
        case 5:
            weekStr = @"周四";
            break;
        case 6:
            weekStr = @"周五";
            break;
        case 7:
            weekStr = @"周六";
            break;
    }
    return weekStr;
}
- (NSInteger)getNumberOfDaysInCurrentMonth {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    NSRange range =  [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self];
#pragma clang diagnostic pop
    return range.length;
}
+ (NSInteger)numberOfDaysInCurrentMonthWithFormatStr:(NSString *)formaStr {
    NSDateFormatter *forma =[[ NSDateFormatter alloc]init];
    forma.dateFormat = @"yyyy-MM";
    return  [[forma dateFromString:formaStr] getNumberOfDaysInCurrentMonth];
    
}
- (NSDate *)getDayInTheFollowingDay:(int)day {
    NSDateComponents *component = [[NSDateComponents alloc]init];
    component.day  = day;
    return  [[NSCalendar currentCalendar] dateByAddingComponents:component toDate:self options:0];
}
- (NSDate *)getFirstDayOfCurrentMonth {
    NSDate *startDate = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self];
//    NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
    #pragma clang diagnostic pop
    return startDate;
}
//获取年月日对象
- (NSDateComponents *)getYMDComponents {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    return [[NSCalendar currentCalendar] components:
            NSYearCalendarUnit|
            NSMonthCalendarUnit|
            NSDayCalendarUnit|
            NSWeekdayCalendarUnit fromDate:self];
    #pragma clang diagnostic pop
}
//获取当前日期之后的几个月
- (NSDate *)dayInTheFollowingMonth:(int)month
{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = month;
    return [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0];
}
//上一个月
- (NSDate *)dayInTheLastMonth {
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    return [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0];
}

//下一个月
- (NSDate *)dayInTheNextMonth {
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = 1;
    return [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0];
}

@end
